package com.gxa.common.utils;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileListUtils {
    public static String upData(List<MultipartFile> files, HttpServletRequest request) throws IOException {
        StringBuilder name = new StringBuilder();
        List<String> list = new ArrayList<>();
        int a = 1;
        System.out.println(0);
        if(files != null && files.size()>0){
            System.out.println(1);
            for (MultipartFile file:files) {
                if(!file.isEmpty()){
                    String uploadPath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
                    File file01 = new File(uploadPath);
                    if(!file01.exists()){
                        file01.mkdirs();
                    }
                    String fileName = file.getOriginalFilename();
                    File newFile = new File(uploadPath + fileName);
                    //上传
                    FileCopyUtils.copy(file.getBytes(),newFile);
                    System.out.println(newFile.toString());
                    try {
                        list.add(newFile.toString());
                    }catch (NullPointerException e){
                        e.printStackTrace();
                    }

                }
            }
            for (String str:list) {
                if(a != list.size()){
                    name.append(str).append("$");
                }else {
                    name.append(str);
                }
                a++;
            }

        }
        return name.toString();
    }
}
